package com.woniu.user.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.woniu.user.domain.DeptAndDoctorName;
import com.woniu.user.domain.DoctorUpList;
import com.woniu.user.domain.Role;
import com.woniu.user.domain.*;
import com.woniu.user.service.IDeptService;
import com.woniu.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.user.service.IDoctorService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import static com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS.required;

/**
 * 工作人员表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController extends BaseController {
    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IRoleService roleService;


    @GetMapping("/getDoctorLikeLists")
    public AjaxResult getDoctorLikeLists(){
        List<Doctor> doctors = doctorService.getDoctorLikeLists();
        return success(doctors);
    };


    @GetMapping("/getDoctorNameAndDeptName/{doctorId}")
    public DeptAndDoctorName getDoctorNameAndDeptName(@PathVariable Long doctorId){
        DeptAndDoctorName andDoctorName = doctorService.getDoctorNameAndDeptName(doctorId);
        return andDoctorName;
    };



    @RequiresPermissions("user:doctor:list")
    @GetMapping("/getDoctorList")
    public AjaxResult getDoctorList(){
        return success(doctorService.getDoctorList());
    };

    @RequiresPermissions("user:doctor:list")
    @GetMapping("/getDoctorListYan")
    public AjaxResult getDoctorListYan(){
        return success(doctorService.getDoctorListYan());
    };

    /**
     * 查询工作人员表列表
     */
    @RequiresPermissions("user:doctor:list")
    @GetMapping("/list")
    public TableDataInfo list(Doctor doctor)
    {
        startPage();
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        for (Doctor item:list) {
             Long roleId = item.getRoleId();
             Role role = roleService.selectRoleByRoleId(roleId);
            item.setRoleName(role.getRoleName());
            Long deptId = item.getDeptId();
            item.setDeptName(deptService.selectDeptByDeptId(deptId).getDeptName());
        }
        return getDataTable(list);
    }

    /**
     * 导出工作人员表列表
     */
    @RequiresPermissions("user:doctor:export")
    @Log(title = "工作人员表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Doctor doctor)
    {
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        util.exportExcel(response, list, "工作人员表数据");
    }

    /**
     * 获取工作人员表详细信息
     */
    @RequiresPermissions("user:doctor:query")
    @GetMapping(value = "/{doctorId}")
    public AjaxResult getInfo(@PathVariable("doctorId") Long doctorId)
    {
        return success(doctorService.selectDoctorByDoctorId(doctorId));
    }

    @RequiresPermissions("user:doctor:query")
    @GetMapping(value = "test/{doctorId}")
    public Doctor selDoctorById(@PathVariable("doctorId") Long doctorId)
    {
        Doctor doctor = doctorService.selectDoctorByDoctorId(doctorId);
        return doctor;
    }

    @RequiresPermissions("user:doctor:query")
    @GetMapping(value = "doctorName/{doctorId}")
    public String selectDoctoeNameById(@PathVariable("doctorId") Long doctorId)
    {
        String doctor = doctorService.getDoctorNameById(doctorId);
        return doctor;
    }

    @RequiresPermissions("user:doctor:query")
    @GetMapping("doctorIdByName/{doctorName}")
    public Doctor getDoctorIdByName(@PathVariable("doctorName") String doctorName)
    {
        Doctor doctor = doctorService.getDoctorIdByName(doctorName);
        return doctor;
    }
    /**
     * 新增工作人员表
     */
    @RequiresPermissions("user:doctor:add")
    @Log(title = "工作人员表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.insertDoctor(doctor));
    }

    /**
     * 修改工作人员表
     */
    @RequiresPermissions("user:doctor:edit")
    @Log(title = "工作人员表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.updateDoctor(doctor));
    }

    /**
     * 删除工作人员表
     */
    @RequiresPermissions("user:doctor:remove")
    @Log(title = "工作人员表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{doctorIds}")
    public AjaxResult remove(@PathVariable Long[] doctorIds)
    {
        return toAjax(doctorService.deleteDoctorByDoctorIds(doctorIds));
    }

    @GetMapping("/dd/{id}")
    public Doctor selectById(@PathVariable("id") long id){
        return doctorService.selectById(id);
    }

    @GetMapping("/filtrate1/{id}")
    public List<Doctor> filtrate1(@PathVariable("id") long id){
        return doctorService.filtrate(id, 7);
    }
    @GetMapping("/filtrate2/{id}")
    public List<Doctor> filtrate2(@PathVariable("id") long id){
        return doctorService.filtrate(id, 6);
    }


    /**
     * 排班系统
     */
    @PostMapping("/paiban")
    public TableDataInfo paiban(@RequestBody(required = false)  TjDto tjDto){
        if (tjDto.getDate()==null){
            tjDto.setDate("当日");
        }
        if(tjDto.getTime()==null)
        {

            List<paibanVo> paibanVo = doctorService.paibanEnd(tjDto.getDate(),Integer.parseInt(tjDto.getId()));
            TableDataInfo dataTable = getDataTable(paibanVo);
            return  dataTable;
        }
        else {
            List<paibanVo> paibanVo = doctorService.paibanEnd2(tjDto.getDate(),tjDto.getTime(),Integer.parseInt(tjDto.getId()));
            TableDataInfo dataTable = getDataTable(paibanVo);
            System.out.println("cotroller"+dataTable.getRows().toString());
            return  dataTable;
        }

    }
}
